$(function () {
    $('#res_img');
    $("#upload_0").uploadifive({
        'uploadScript': uploader,
        'buttonText': '选择图片',
        'auto': true,
        '_token': "{{ csrf_token() }}",
        'multi': false,//是否多文件上传
        'width': 120,
        'fileTypeExts': '*.gif; *.jpg; *.png; *.flv',	//允许上传的文件后缀
        'fileSizeLimit': '8MB',					//上传文件的大小限制，单位为B, KB, MB, 或 GB
        'onUploadComplete': function (file, data) {
            /*setTimeout(function () {
                $('#uploadifive-one_img_upload-queue').hide();
            }, 2000);*/
            $('#uploadifive-upload_0-queue').hide();
            data = JSON.parse(data);
            if (data.error != '') {
                layer.confirm(data.error, {icon: 3, title: '提示', offset: '100px'}, function (index) {
                    $('#res_img').html('');
                    layer.close(index);
                });
            } else {
                getResult_one_img(data, this); //获得img的ID
            }
        }
    });

    $("#upload_1").uploadifive({
        'uploadScript': uploader,
        'buttonText': '选择图片',
        'auto': true,
        '_token': "{{ csrf_token() }}",
        'multi': false,//是否多文件上传
        'width': 120,
        'fileTypeExts': '*.gif; *.jpg; *.png; *.flv',	//允许上传的文件后缀
        'fileSizeLimit': '8MB',					//上传文件的大小限制，单位为B, KB, MB, 或 GB
        'onUploadComplete': function (file, data) {
            /*setTimeout(function () {
             $('#uploadifive-one_img_upload-queue').hide();
             }, 2000);*/
            $('#uploadifive-upload_1-queue').hide();
            data = JSON.parse(data);
            if (data.error != '') {
                layer.confirm(data.error, {icon: 3, title: '提示', offset: '100px'}, function (index) {
                    $('#res_img').html('');
                    layer.close(index);
                });
            } else {
                getResult_one_img(data, this); //获得img的ID
            }
        }
    });

    $("#upload_2").uploadifive({
        'uploadScript': uploader,
        'buttonText': '选择图片',
        'auto': true,
        '_token': "{{ csrf_token() }}",
        'multi': false,//是否多文件上传
        'width': 120,
        'fileTypeExts': '*.gif; *.jpg; *.png; *.flv',	//允许上传的文件后缀
        'fileSizeLimit': '8MB',					//上传文件的大小限制，单位为B, KB, MB, 或 GB
        'onUploadComplete': function (file, data) {
            /*setTimeout(function () {
             $('#uploadifive-one_img_upload-queue').hide();
             }, 2000);*/
            $('#uploadifive-upload_2-queue').hide();
            data = JSON.parse(data);
            if (data.error != '') {
                layer.confirm(data.error, {icon: 3, title: '提示', offset: '100px'}, function (index) {
                    $('#res_img').html('');
                    layer.close(index);
                });
            } else {
                getResult_one_img(data, this); //获得img的ID
            }
        }
    });

    $("#upload_3").uploadifive({
        'uploadScript': uploader,
        'buttonText': '选择图片',
        'auto': true,
        '_token': "{{ csrf_token() }}",
        'multi': false,//是否多文件上传
        'width': 120,
        'fileTypeExts': '*.gif; *.jpg; *.png; *.flv',	//允许上传的文件后缀
        'fileSizeLimit': '8MB',					//上传文件的大小限制，单位为B, KB, MB, 或 GB
        'onUploadComplete': function (file, data) {
            /*setTimeout(function () {
             $('#uploadifive-one_img_upload-queue').hide();
             }, 2000);*/
            $('#uploadifive-upload_3-queue').hide();
            data = JSON.parse(data);
            if (data.error != '') {
                layer.confirm(data.error, {icon: 3, title: '提示', offset: '100px'}, function (index) {
                    $('#res_img').html('');
                    layer.close(index);
                });
            } else {
                getResult_one_img(data, this); //获得img的ID
            }
        }
    });

    $("#upload_4").uploadifive({
        'uploadScript': uploader,
        'buttonText': '选择图片',
        'auto': true,
        '_token': "{{ csrf_token() }}",
        'multi': false,//是否多文件上传
        'width': 120,
        'fileTypeExts': '*.gif; *.jpg; *.png; *.flv',	//允许上传的文件后缀
        'fileSizeLimit': '8MB',					//上传文件的大小限制，单位为B, KB, MB, 或 GB
        'onUploadComplete': function (file, data) {
            /*setTimeout(function () {
             $('#uploadifive-one_img_upload-queue').hide();
             }, 2000);*/
            $('#uploadifive-upload_4-queue').hide();
            data = JSON.parse(data);
            if (data.error != '') {
                layer.confirm(data.error, {icon: 3, title: '提示', offset: '100px'}, function (index) {
                    $('#res_img').html('');
                    layer.close(index);
                });
            } else {
                getResult_one_img(data, this); //获得img的ID
            }
        }
    });

    //添加时候的图片删除
    function getResult_one_img(img_url, obj) {
        var sort = $(obj).parents('li').index();
        var type = 'res_img_' + sort;
        console.log(img_url,obj);
        /*var str = '<li><div style="margin: 5px"><div id="' + type + '"></div><div style="margin-top: 5px"><input type="text" placeholder="描述" name="name[]" style="width: 100%" required></div><div style="margin-top: 5px"><input type="text" placeholder="链接" name="link[]" style="width: 100%" required onblur="checkUrl(this)"></div><div style="margin-top: 5px;height: 30px;line-height: 30px"> <a onclick="changeSort(this,1)" style="margin:5px;"><i class="ace-icon fa fa-angle-double-left"></i></a><a onclick="changeSort(this,2)" style="margin:5px;"><i class="ace-icon fa fa-angle-double-right"></i></a><a style="margin:5px;" onclick="remove(this)"><i class="ace-icon glyphicon glyphicon-remove"></i></a><span style="float: right">尺寸：750x400</span></div></div></li>';
        $(obj).parents('li').before(str);*/
        //var type = "res_img";
        var img_url_sort = 'img_url_' + sort;
        $('#' + type).html('');
        $("#" + type).append('<div id="' + img_url_sort + '"></div>');
        $("#" + img_url_sort).append("<img src=" + img_url.path + "  width = '240' height = '180'>");
        $("#" + img_url_sort).append("<input value=" + img_url.image_id + " name='img_ids[]' type='hidden'>");
        // $("#" + img_url_sort).append("<div id='near_img_" + sort + "' onmouseout='del3(this)' onclick='delimage()' class='innerdiv'>删除</div>");
    }
});


function del1(me) {
    var i = $(me).parent().attr("id");
    $("#" + i + " .innerdiv").css("top", "-212px");
}

function del2(data, a) {
    if (!a) {
        a = 'img_div';
    }
    $("#divTxt input[value='" + data + "']").remove();
    $("#" + a + data).remove();
}

function del3(me) {
    $(me).css("top", "210px");
}

function delimage() {
    layer.confirm('确认删除图片？', {icon: 3, title: '提示', offset: '100px'}, function (index) {
        $('#res_img').html('');
        layer.close(index);
    });

}